"""登录页面对象"""
import time

from selenium.webdriver.common.by import By
import allure
from base.base import BasePage, BaseHandler


class LoginPage(BasePage):
    def __init__(self):
        super().__init__()
        self.phone = By.XPATH, '//input[@placeholder="用户名/邮箱/手机号"]'
        self.pswd = By.XPATH, '//input[@placeholder="登录密码"]'
        self.login = By.XPATH, '//input[@value="立即登录"]'
        # 提示信息
        self.msg = By.XPATH, '//span[@ng-bind="loginErr"]'

    def phone_elem(self):
        return self.get_element(self.phone)

    def pswd_elem(self):
        return self.get_element(self.pswd)

    def login_elem(self):
        return self.get_element(self.login)

    def msg_elem(self):
        return self.get_element(self.msg)


class LoginHandler(BaseHandler):
    def __init__(self):
        self.login_page = LoginPage()

    @allure.step(title="输入手机号")
    def input_phone(self, text):
        self.input_text(self.login_page.phone_elem(), text)

    @allure.step(title="输入密码")
    def input_pswd(self, text):
        self.input_text(self.login_page.pswd_elem(), text)

    @allure.step(title="点击登录")
    def click_login(self):
        self.login_page.login_elem().click()

    def get_msg(self):
        return self.login_page.msg_elem().text


class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandler()

    def login(self, phone_text, pswd_text):
        self.login_handle.input_phone(phone_text)
        self.login_handle.input_pswd(pswd_text)
        self.login_handle.click_login()

    def get_login_msg(self):
        time.sleep(1)
        msg = self.login_handle.get_msg()
        return msg
